<?php

//print "<pre>";print_r($_POST);print"</pre>";
//print "<pre>Submit<br/>";print_r($_SESSION);print"</pre>";
if(isset($_POST['reset'])){
	unset($_SESSION['form']);
}
if(isset($_POST['removeInterface'])){

	$num = $_POST['removeInterface'];
	unset($_SESSION['form']['iface'][$num]);

}
if(isset($_POST['removeAddress'])){

	$num = $_POST['removeAddress'];
	unset($_SESSION['form']['iface']['ipconfigs'][$num]);

}
if(!empty($_POST)){

	$_SESSION['form']['server']["name"] 		= $_POST['name'];
	$_SESSION['form']['server']["description"]	= $_POST['description'];
	$_SESSION['form']['server']["client"] 		= $_POST['client'];

	switch($_POST['hSelect']){
		case "1":
			$_SESSION['form']['server']["hid"] = "";
			break;
		case "2":
			$_SESSION['form']['server']["hid"] = $_POST['hostMachine'];
	}

}
if(isset($_POST['saveConfiguration'])){
	
	if(	!empty($_POST['intType']) 	&& !empty($_POST['address']) && !empty($_POST['gateway']) && !empty($_POST['addIPtoINT'])){
		
			$_SESSION['form']['iface'][$_POST['addIPtoINT']]['ipconfigs'][] = array("tid" => 		$_POST['intType'],
																					"address" => 	$_POST['address'],
																					"netmask" => 	$_POST['netmask'],
																					"gateway" => 	$_POST['gateway'],
																					"pid" =>		$_POST['prefix']);
	}else{

		$_SESSION['error']['servercreate'] = "Failed to create IP configuration";
		print "<pre>";print_r($_POST);print"</pre>";
		
	}
}
/*
 * Save the interface if the saveInterface button was pressed
 */
if(isset($_POST['saveInterface']) || isset($_POST['createAuth'])){

	$i = 1;

	if(isset($_SESSION['form'])){

		$i = count($_SESSION['form']['iface']);
		$i++;

	}

	if(	!empty($_POST['vlan']) 	&& !empty($_POST['network']) && !empty($_POST['interface']) && !empty($_POST['mac'])){

		switch($_POST['hSelect']){
			case "1":
				$_SESSION['form']['server']["hid"] = NULL;
				break;
			case "2":
				$_SESSION['form']['server']["hid"] = $_POST['hostMachine'];
		}

		$_SESSION['form']['iface'][$i]['settings'] =  array("vid" => 		$_POST['vlan'],
															"nid" => 	$_POST['network'],
															"interface" => 	$_POST['interface'],
															"mac" => 		$_POST['mac']);		

	}else{

		$_SESSION['error']['servercreate'] = "Failed to create interface";
		print "<pre>";print_r($_POST);print"</pre>";
		
	}

		
}

/*
 * Create the server if the createAuth button was pressed
 */
if(isset($_POST['createAuth']) && isset($_SESSION['form'])){
	$server = new LPCPServer();
	/*
	 * START THE MYSQL TRANSACTION
	 */
	$lpcp->getLPCPServer()->startTransaction();
	
	$serverID = $server->createServer($_SESSION['form']['server']);
	
	if($serverID){

		$i = 1;
		//print"<pre>";print_r($_SESSION['form']);print"</pre>";
		foreach($_SESSION['form']['iface'] as $interface){
			//print"<pre>";print_r($interface);print"</pre>";
			
			$interface['settings']['sid'] = $serverID;
			
			//if(!$lpcp->getLPCPServer()->createInterface($interface)){
			$iid = $server->createInterface($interface['settings']);
			
			if(!$iid){

				$_SESSION['error']['servercreate'] = "Failed to create server interface";
				$error = true;
				
				// KILL THE TRANSACTION AND REVERT CHANGES
				$lpcp->getLPCPServer()->rollbackTransaction();
				break;

			}
			
			foreach($interface['ipconfigs'] as $ipArr){

				$ipArr['iid'] = $iid;
				if(!$server->createIPConfig($ipArr)){
	
					$_SESSION['error']['servercreate'] = "Failed to create server ip config";
					$error = true;
	
					// KILL THE TRANSACTION AND REVERT CHANGES
					$lpcp->getLPCPServer()->rollbackTransaction();
					break;
					
				}
	
			}

		}




		if(!$error){

			print "<i id='success'>Successfully created server!</i>";
			//unset($_SESSION['error']);
			//unset($_SESSION['form']);

			
			 //COMMIT THE TRANSACTION
			 
			$lpcp->getLPCPServer()->commitTransaction();

		}

	}else{

		$_SESSION['error']['servercreate'] = "Failed to create server";
		
		// KILL THE TRANSACTION AND REVERT CHANGES
		$lpcp->getLPCPServer()->rollbackTransaction();
		//break;

	}

}
?>